package com.mes.controller.warehouse;

import com.interf.WCSUtils;
import com.mes.entity.User;
import com.mes.service.manu.FlyWeavService;
import com.mes.service.warehouse.DdXbkSjListService;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 大底线边库上架
 */
@Controller
@RequestMapping("ddxbkSjList")
public class DdXbkSjListController {
    @Autowired
    private DdXbkSjListService ddXbkSjListService;
    @Autowired
    private FlyWeavService flyWeavService;


    //通过RFID工单号及数量存入中间表
    @ResponseBody
    @RequestMapping(value="saveXbkSjListByCode",method= RequestMethod.POST)
    public Message saveXbkSjListByCode(HttpServletRequest request){
        Message msg = new Message();
        try{

            String leftcode = request.getParameter("leftcode").toString();   //左脚RFID
            String rightcode = request.getParameter("rightcode").toString();   //右脚RFID
            String leftRfid = leftcode.replaceAll(" ", "");
            String rightRfid = rightcode.replaceAll(" ", "");


            List<Map<String, Object>> LeftProduct = ddXbkSjListService.getBatchnorLists(leftRfid); //传过来的是左脚RFID号

            List<Map<String, Object>> RightProduct = ddXbkSjListService.getBatchnorLists(rightRfid); //传过来的右脚RFID号


            if(LeftProduct.size() == 0  ){
                msg.setData("LeftxbkSjListByCode","false");
                return msg;
            }

            if( RightProduct.size() == 0 ){
                msg.setData("RightxbkSjListByCode","false");
                return msg;
            }

            List<Map<String, Object>> mescode = null;
            String ckCode = "";

            //左脚信息
            String LeftWLVALUE = LeftProduct.get(0).get("WLVALUE").toString();  //物料编码
            String LeftWLNAME = LeftProduct.get(0).get("WLNAME").toString();  //物料名称
            String LeftWLID = LeftProduct.get(0).get("WLID").toString();  //物料ID
            String LeftYSID = LeftProduct.get(0).get("YSID").toString();  //颜色ID
            String LeftCMID = LeftProduct.get(0).get("CMID").toString();  //尺码ID
            String LeftPCID = LeftProduct.get(0).get("BATCHID").toString();  //WMS_STORAGE_BATCH_ID
            String LeftITEMID = LeftProduct.get(0).get("ITEMID").toString();  //WMS_STORAGE_BATCH_ITEM_ID

            //右脚信息
            String RightWLID = RightProduct.get(0).get("WLID").toString();  //物料ID
            String RightYSID = RightProduct.get(0).get("YSID").toString();  //颜色ID
            String RightCMID = RightProduct.get(0).get("CMID").toString();  //尺码ID
            String RightPCID = RightProduct.get(0).get("BATCHID").toString();  //WMS_STORAGE_BATCH_ID
            String RightITEMID = RightProduct.get(0).get("ITEMID").toString();  //WMS_STORAGE_BATCH_ITEM_ID


            //判断左脚RFID是否和右脚RFID的物料类型尺码颜色是否一致
            if(!LeftPCID.equals(RightPCID)){
                msg.setData("matching","false");
                return msg;
            }

            String type = "大底";
            String Status = "CREATE";
            mescode = ddXbkSjListService.selectWarehouseCode(LeftWLID);

            ckCode = mescode.get(0).get("CKCODE").toString();  //MES参数编码（仓库编码）

            String rfid = "";
            rfid =leftRfid+","+rightRfid;

            User user = UserUtil.getUser();
            ddXbkSjListService.inStorageUp(LeftPCID , type,  rfid , user, Status,  LeftWLID , LeftCMID , LeftYSID,LeftITEMID,RightITEMID,leftRfid,rightRfid);
            //调用接口往中间表插数据
           WCSUtils.inStorck(ckCode, rfid, rfid, LeftWLVALUE, LeftWLNAME, 1);

        }catch (Exception e){
            msg.setData("result","false");
            e.printStackTrace();
        }
        return msg;
    }
}
